<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['regId']) && isset($_POST['name'])) {
    
    $regId = $_POST['regId'];
    $name = $_POST['name'];
	
    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
	$check = mysql_query("SELECT *FROM gcm_users WHERE regId = $regId");
	if(mysql_fetch_array($check) !== false)
	{
        //update 
    $result = mysql_query("UPDATE gcm_users SET name = '$name' WHERE gcm_regid = $regId");
    
	}
	else{
	$result = mysql_query("INSERT INTO gcm_users(gcm_regid, name) VALUES('$regId', '$name')");
	}

    // check if row inserted or not
    if ($result) {
       
    // echoing JSON response

        $response["responseCode"] = 1;
        $response["responseMessage"] = "Successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["responseCode"] = -1;
        $response["responseMessage"] = "Oops! An error occurred.";
        
        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["responseCode"] = -1;
    $response["responseMessage"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>